package ltd.bugs.tool.signature.builder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

import ltd.bugs.tool.signature.attribute.SignatureAttribute;
import ltd.bugs.tool.signature.factory.Signature;
import ltd.bugs.tool.signature.factory.SignatureFactory;
import ltd.bugs.tool.signature.factory.SignatureTypes;
import ltd.bugs.tool.signature.formatter.ResultFormatUtils;
import ltd.bugs.tool.signature.formatter.ResultFormatter;
import ltd.bugs.tool.signature.utils.StringUtils;

/**
 * @author zhengzhq
 * @Title: SignatureBuilder
 * @Description: 采用builder模式进行签章的生成，根据需要进行订制
 * @date 2018/8/9下午4:12
 */
public class SignatureBuilder {

  private static Logger logger = LoggerFactory.getLogger(SignatureBuilder.class);

  // 要签名的文本
  private String text;
  // 印章的色调
  private Color color;
  // 印章宽度
  private int width;
  // 印章高度
  private int height;
  // 印章字体
  private Font font;
  // 印章类型
  private SignatureTypes type;
  // 扩展信息，如果印章中需要一些扩展信息，请通过扩展参数传递
  private Map<String, Object> extras;
  // 印章处理器，默认base64
  private ResultFormatter formatter = ResultFormatter.BASE64;

  static {
    logger.debug("now will set system property 'java.awt.headless' to hide x11 server!");
    // 以非图形界面生成。避免没有安装图形界面的linux服务器报错，另外也可以提高性能
    System.setProperty("java.awt.headless", "true");
  }

  private SignatureBuilder() {

  }

  public static SignatureBuilder newBuilder() {
    return new SignatureBuilder();
  }

  public String draw() {
    // 校验基本参数的准确性
    checkParam();

    // 根据请求的印章类型获取对应的实现节点
    Signature signature = SignatureFactory.getInstance(type);

    // 开始绘制签章
    BufferedImage image = signature.draw(font, color, width, height, extras);

    return ResultFormatUtils.format(formatter, image, extras);
  }

  private void checkParam() {
    if (type == null) {
      throw new IllegalArgumentException("type can't be null.");
    }

    if (font == null) {
      throw new IllegalArgumentException("font can't be null.");
    }

    // 如果未设置颜色，默认红色。绝大多数的章也是红色的
    if (color == null) {
      logger.debug("color is null, will use red color instead!");
      color = Color.RED;
    }

    // 宽和高是必须设置的
    if (height <= 0 || width <= 0) {
      throw new IllegalArgumentException("width or height incorrect, please check again!");
    }

    if (!StringUtils.isEmpty(text)) {
      if (extras == null) {
        extras = new HashMap<String, Object>();
      }
      extras.put(SignatureAttribute.SIGNATURE_TEXT, text);
    }
  }

  public Color getColor() {
    return color;
  }

  public SignatureBuilder setColor(Color color) {
    this.color = color;
    return this;
  }

  public int getWidth() {
    return width;
  }

  public SignatureBuilder setWidth(int width) {
    this.width = width;
    return this;
  }

  public int getHeight() {
    return height;
  }

  public SignatureBuilder setHeight(int height) {
    this.height = height;
    return this;
  }

  public Font getFont() {
    return font;
  }

  public SignatureBuilder setFont(Font font) {
    this.font = font;
    return this;
  }

  public SignatureTypes getType() {
    return type;
  }

  public SignatureBuilder setType(SignatureTypes type) {
    this.type = type;
    return this;
  }

  public Map<String, Object> getExtras() {
    return extras;
  }

  public SignatureBuilder setExtras(Map<String, Object> extras) {
    this.extras = extras;
    return this;
  }

  public SignatureBuilder setFormatter(ResultFormatter formatter) {
    this.formatter = formatter;
    return this;
  }

  public SignatureBuilder setText(String text) {
    this.text = text;
    return this;
  }
}
